1 Background

The R library crosstalk is an increasingly popular tool in the realm of web development, especially in the context of data analysis and visualization.

Crosstalk is a JavaScript library for two-way data binding between widgets in HTML, making it easier to create coordinated, interactive visualizations.

Furthermore, crosstalk facilitates synchronization and communication between different HTML widgets or visualizations, ensuring that interactions in one widget affect others.

This library is often used by web developers and data analysts who want to create interactive visualizations that are synchronized across multiple components.

The main advantages of crosstalk are:

In today’s post, we will practice the R library crosstalk using a dataset on number of animals slaughtered for meat worldwide retrieved from the website “Our World in Data” (https://ourworldindata.org/animal-welfare). For the sake of simplification, we will retain the top 3 world’s largest meat producers in 2021.

Therefore, we will practice the following skills:

  1. Web scraping
  2. Data processing (filtering, grouping, mutating, merging)
  3. Data summary and table creation
  4. Interactive table and plot creation using crosstalk
  5. Data visualization

2 Data preparation

2.1 Web scraping

## # A tibble: 199 × 4
##     Rank Country       `Meat production(in tonnes)`  Year
##    <int> <chr>         <chr>                        <int>
##  1     1 China         90,737,304                    2021
##  2     2 United States 48,876,684                    2021
##  3     3 Brazil        29,497,016                    2021
##  4     4 Russia        11,346,122                    2021
##  5     5 India         10,888,240                    2021
##  6     6 Mexico        7,692,412                     2021
##  7     7 Spain         7,649,473                     2021
##  8     8 Germany       7,632,081                     2021
##  9     9 Argentina     6,152,519                     2021
## 10    10 France        5,363,326                     2021
## # ℹ 189 more rows

First, we search for the ranking of world’s largest meat producers. This can be found on https://en.wikipedia.org/wiki/List_of_countries_by_meat_production.

And with that we can start retrieving the data from the selected table.

Initially, all tables present in the webpage will be listed in R.

We then just need to select the table we need.

Thereafter, we can retrieve the names of the top 3 meat producers (China, United States, Brazil) and include them into a vector, which will be used to filter the observations we will use further in this exercise.

2.2 Data loading, preparation, and summary

The data on the number of land animals slaughtered for meat per year between 1961 and 2021 was previously downloaded as csv. format from the “Our World in Data” website (https://ourworldindata.org/animal-welfare).

After uploading it in R, the observations from the 3 selected countries were filtered out in one sub-dataset with 183 yearly observations.

Then, the yearly average number of land animals slaughtered was calculated using the R library skimr and displayed in Table 1 using the R library flextable.

Table 1: Yearly average number of land animals slaughtered in the world’s top 3 meat producers between 1961 and 2021.

Country

Average

United States

6,469,932,188

China

6,041,165,848

Brazil

2,680,708,955

3 Creating a crosstalk app including interactive table and plot

Our crosstalk app contains:

  1. Selection boxes for “country” and “year” helping you filtering desired rows and information;

  2. Table displaying the absolute number of land animals slaughtered by China, United States, Brazil between 1961 and 2021;

  3. Figure illustrating the variation in percentage of land animals slaughtered for meat from 1961 to 2021.

You can play with the example below by manipulating the selection boxes, clicking rows in the data table, and playing with the selection button in the figure.

4 Conclusions

4.1 Animal welfare

Although this article is about data science skills, we can’t close our eyes to the enormous amount of land animals slaughtered for meat every year worldwide!

Only Brazil, China, USA slaughtered over 32 billion land animals for meat!

Additionally, the evolution in the number of land animals slaughtered for meat between 1961 and 2021 is also impressive, especially in Brazil and China with increases of 4254% and 2730%, respectively.

These sizable figures indicate substantial opportunities to conduct research and improve animal welfare and health!

4.2 Stats

If you reached this point, you learned web scraping, data loading, handling and processing, and creating a interactive table and plot using the R library crosstalk. Congrats!

Crosstalk is excellent for creating synchronized and interactive HTML widgets.

Similar to standard HTML widgets, Crosstalk operates independently of Shiny or any additional R runtime dependencies.

This independence allows you to employ it either in isolation or in conjunction with R Markdown for producing static HTML documents that are easily hostable on various platforms like Amazon S3, GitHub, or RPubs.

Nevertheless, it’s intentionally built to seamlessly integrate with Shiny applications, enabling the fusion of Crosstalk widget interactions with an array of elements including base plots, ggplot2, and the full spectrum of Shiny functionalities.

See you and enjoy coding!